/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/27/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define MatHangTonKhoDao class. This class inherit from  MatHangTonKhoDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class MatHangTonKhoDao : MatHangTonKhoDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public MatHangTonKhoDao() { }
        static MatHangTonKhoDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(MatHangTonKhoDao).FullName);
        }
        public static bool RemoveMatHangTonKho(MatHangTonKho obj)
        {
            bool result = false;
            String methodName = "spRemoveMatHangTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spRemoveMatHangTonKho");

            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32, obj.MaKho);
            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString, obj.MaMatHang);
            //Add value "SoLuongTon"  paramater.
            ManagerDao.AddInParameter(cmd, "SoLuongTon", DbType.Int64,
                ConvertValueObject.CastObjValueToInsertDB(obj.SoLuongTon));
            //Add value "MaDonViTinh"  paramater.
            ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32, obj.MaDonViTinh);
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        public static bool RollBackMatHangTonKho(MatHangTonKho obj)
        {
            bool result = false;
            String methodName = "RollBackMatHangTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spRollbackSoLuongTonForPhieuBanHang");

            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32, obj.MaKho);
            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString, obj.MaMatHang);
            //Add value "SoLuongTon"  paramater.
            ManagerDao.AddInParameter(cmd, "RollbackCount", DbType.Int64, obj.SoLuongTon);
            //Add value "MaDonViTinh"  paramater.
            ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32, obj.MaDonViTinh);
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        public static List<ChiTietTonKho> GetThongKeTonKho(int maKho, int maloaiHH, int chungXe, int maXe, string maMH)
        {
            String methodName = "GetBaoCaoTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietTonKho> listMatHangTonKho = new List<ChiTietTonKho>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spThongKeTonKho");
                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32, maKho);
                ManagerDao.AddInParameter(cmd, "MaLoaiHH", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(maloaiHH));
                ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(chungXe));
                ManagerDao.AddInParameter(cmd, "MaXe", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(maXe));
                ManagerDao.AddInParameter(cmd, "MaMH", DbType.String, ConvertValueObject.CastObjValueToInsertDB(maMH));
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listMatHangTonKho.Add(GetChiTietTonKhoByDataReader(reader));
                    }

                    reader.Close();
                }

                return listMatHangTonKho;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        public static List<ChiTietTonKho> GetThongKeTonKho(int maKho, int chungXe, int maXe, string maMH,string maPT,string orderBy,string orderDirection,int pIndex,int pSize, ref int total)
        {
            String methodName = "GetThongKeTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietTonKho> listMatHangTonKho = new List<ChiTietTonKho>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spThongKeMatHangTonKho_V3");
                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(maKho));                
                ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(chungXe));
                ManagerDao.AddInParameter(cmd, "MaXe", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(maXe));
                ManagerDao.AddInParameter(cmd, "MaMH", DbType.String, ConvertValueObject.CastObjValueToInsertDB(maMH));
                ManagerDao.AddInParameter(cmd, "MaPhuTung", DbType.String, ConvertValueObject.CastObjValueToInsertDB(maPT));
              
                ManagerDao.AddInParameter(cmd, "OrderBy", DbType.String, orderBy);
                ManagerDao.AddInParameter(cmd, "OrderDirection", DbType.String, orderDirection);
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, pIndex);
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32, pSize);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listMatHangTonKho.Add(GetChiTietTonKhoByDataReader(reader,ref total));
                    }

                    reader.Close();
                }

                return listMatHangTonKho;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        private static ChiTietTonKho GetChiTietTonKhoByDataReader(IDataReader reader)
        {
            String methodName = "GetChiTietTonKhoByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                ChiTietTonKho result = new ChiTietTonKho();
                //read DonViTinh field data.
                result.DonViTinh = ManagerDao.GetstringDataFromDataReader(reader, "DonViTinh");
                //read Name field data.
                result.Name = ManagerDao.GetstringDataFromDataReader(reader, "Name");
                result.TenChungXe = ManagerDao.GetstringDataFromDataReader(reader, "TenChungXe");
                result.TenXe = ManagerDao.GetstringDataFromDataReader(reader, "TenXe");

                //read Id field data.
                result.Id = ManagerDao.GetstringDataFromDataReader(reader, "Id");

                //read SoLuongTon field data.
                result.SoLuongTon = ManagerDao.Getint64DataFromDataReader(reader, "SoLuongTon");
                result.MaDVT = ManagerDao.GetintDataFromDataReader(reader, "maDVT");

                //read Type field data.
                result.Type = ManagerDao.GetintDataFromDataReader(reader, "Type");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        private static ChiTietTonKho GetChiTietTonKhoByDataReader(IDataReader reader,ref int total)
        {
            String methodName = "GetChiTietTonKhoByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                if(total<1)
                    total = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
                ChiTietTonKho result = new ChiTietTonKho();
                //read DonViTinh field data.
                result.DonViTinh = ManagerDao.GetstringDataFromDataReader(reader, "DonViTinh");
                //read Name field data.
                result.Name = ManagerDao.GetstringDataFromDataReader(reader, "Name");
                result.TenChungXe = ManagerDao.GetstringDataFromDataReader(reader, "TenChungXe");
                result.TenXe = ManagerDao.GetstringDataFromDataReader(reader, "TenXe");

                //read Id field data.
                result.Id = ManagerDao.GetstringDataFromDataReader(reader, "Id");

                //read SoLuongTon field data.
                result.SoLuongTon = ManagerDao.Getint64DataFromDataReader(reader, "SoLuongTon");
                result.MaDVT = ManagerDao.GetintDataFromDataReader(reader, "maDVT");

                //read Type field data.
                //result.Type = ManagerDao.GetintDataFromDataReader(reader, "Type");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static bool CapNhatMatHangTonKho(MatHangTonKho obj, int maPhieuCapNhatTonKho)
        {
            bool result = false;
            String methodName = "CapNhatMatHangTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spCapNhatSoLuongTon_V1");

            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32, obj.MaKho);
            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString, obj.MaMatHang);
            //Add value "SoLuongTon"  paramater.
            ManagerDao.AddInParameter(cmd, "SoLuongTon", DbType.Int64, obj.SoLuongTon);
            //Add value "MaDonViTinh"  paramater.
            ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32, obj.MaDonViTinh);
            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "maPhieuCapNhatTonKho", DbType.Int32, maPhieuCapNhatTonKho);
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        public static bool CapNhatMatHangTonKho(List<MatHangTonKho> obj,PhieuCapNhatTonKho objPhieuCapNhatTonKho)
        {
            
            String methodName = "CapNhatMatHangTonKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            ManagerDao.BeginTransaction();
            int maPhieuCapNhatTonKho = PhieuCapNhatTonKhoDao.Insert(objPhieuCapNhatTonKho);
            if (maPhieuCapNhatTonKho < 1)
            {
                ManagerDao.Rollback();
                return false;
            }
            foreach (MatHangTonKho objData in obj)
            {
                if (!CapNhatMatHangTonKho(objData, maPhieuCapNhatTonKho))
                {
                    ManagerDao.Rollback();
                    return false;
                }
                
            }
            ManagerDao.Commit();
            return true;            
        }
        public static void ThongKeDoanhThuBanHang(string condition, ref int soPhieu,ref double TongTien)
        {
            String methodName = "ThongKeDoanhThuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietTonKho> listMatHangTonKho = new List<ChiTietTonKho>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spThongKeDoanhThuBanHang");
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, condition);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    if(reader.Read())
                    {
                        soPhieu = ManagerDao.Getint32DataFromDataReader(reader, "TongSoPhieu");
                        TongTien = ManagerDao.GetdoubleDataFromDataReader(reader, "TongGiaTien");
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);                
            }
        }
        public static void ThongKeDoanhThuNhapHang(string condition, ref int soPhieu, ref double TongTien)
        {
            String methodName = "ThongKeDoanhThuNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<ChiTietTonKho> listMatHangTonKho = new List<ChiTietTonKho>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spThongKeDoanhThuNhapHang");
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, condition);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    if (reader.Read())
                    {
                        soPhieu = ManagerDao.Getint32DataFromDataReader(reader, "TongSoPhieu");
                        TongTien = ManagerDao.GetdoubleDataFromDataReader(reader, "TongGiaTien");
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
            }
        }
    }
}
        #endregion